package com.translate.web.api.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;

import com.translate.vo.RechargeVO;
import com.response.vo.ResponseModel;
import com.controller.base.AbstractCommonController;
import com.translate.web.param.RechargeUpdateParam;
import com.translate.web.param.DateRangeParam;
import com.translate.web.param.RechargeAddParam;
import com.translate.web.param.contract.IRechargeAddParam;
import com.translate.entity.NoticeCenter;
import com.translate.entity.PayType;
import com.translate.entity.Recharge;
import com.translate.web.service.INoticeCenterService;
import com.translate.web.service.IPayTypeService;
import com.translate.web.service.IPreStoredRewardsConfService;
import com.translate.web.service.IRechargeService;
import com.translate.web.service.IUserService;
import com.basic.service.contract.IBaseService;
import com.common.util.SnowFlake;

@RestController
@RequestMapping("recharge")
public class RechargeController extends AbstractCommonController<Recharge, DateRangeParam> {
	private IRechargeService service;

	private IPreStoredRewardsConfService preStoredRewardsConfService;
	
	private IUserService userService;
	
	private IPayTypeService payTypeService;
	
	INoticeCenterService centerService;
	@Resource
	public void setCenterService(INoticeCenterService centerService) {
		this.centerService = centerService;
	}

	
	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public RechargeController(IRechargeService service) {
		this.service = service;
	}

	protected long setCreateInfo(IRechargeAddParam param, HttpServletRequest httpServletRequest) {
		long userId = this.getUserId(httpServletRequest);
		param.setUserId(userId);
		return userId;
	}

	protected long getUserId(HttpServletRequest httpServletRequest) {
		return (long) httpServletRequest.getAttribute("userId");
	}

	@Override
	protected void frontProcesserVO(DateRangeParam param, NativeWebRequest object) {
		super.frontProcesserVO(param, object);
		param.setUserId((long) object.getAttribute("userId", RequestAttributes.SCOPE_REQUEST));
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/add")
	@Transactional
	public ResponseModel<Integer> insert(@RequestBody @Validated RechargeAddParam param,
			HttpServletRequest httpServletRequest) {
		setCreateInfo(param, httpServletRequest);
		
		PayType payItemFromCache = payTypeService.getPayItemFromCache(param.getPayId());
		
		param.setPyName(payItemFromCache.getTypeName());
		
		Byte rewardType = param.getRewardType();
		String reward = this.preStoredRewardsConfService.getReward(param.getAccount(), rewardType);

		param.setReward(reward);
		// 没有奖励
		long userId = param.getUserId();
		if (reward.equals("")) {
			this.userService.updateDiscount(userId, BigDecimal.valueOf(100));
			// 改变状态
			param.setRewardType((byte) 2);
		} else  {
			if (0 == rewardType) {
				
//				param.setAccount(param.getAccount().multiply(BigDecimal.valueOf(Double.valueOf(reward))).divide(BigDecimal.valueOf(100)));
				// 更新折扣
//				this.userService.updateDiscount(param.getUserId(), Float.valueOf(reward));
			}
		} 
		
		String orderSn = "YC" + new SnowFlake(0, 0).nextId();
		
		param.setOrderSn(orderSn);
		
		int id = this.service.parseInsert(param, Recharge.class);
		
		NoticeCenter center = new NoticeCenter();

		center.setUserId((int) userId);

		// 您的预存金额【¥XXX】单号YC202xxxxxxxxxXX已提交，请耐心等待平台处理
		BigDecimal amount = param.getAccount();
		center.setNoticeContent("您发起的【￥" + amount + "】单号"+ orderSn +"提现申请已提交，请耐心等待平台处理。");

		center.setPlatform((byte) 0);

		center.setNoticeType((byte) 4);

		center.setTitle("预存金额通知");
		
		center.setTypeId(id);

		centerService.insert(center);
		
		
		// 向管理端发送数据
		
		center = new NoticeCenter();

		center.setUserId((int) userId);
		
		Object attribute = httpServletRequest.getAttribute("realname");
		
		// 张三(ID:12345678)预存金额【¥xxX】单号YC202xxxxxxxxxXX，请及时查看处理!
		center.setNoticeContent(attribute + "【ID:" + userId + "】预存金额【￥"+amount+"】" +orderSn +"请及时查看处理!。");

		center.setPlatform((byte) 1);

		center.setNoticeType((byte) 4);

		center.setTitle("预存金额通知");
		
		center.setTypeId(id);

		centerService.insert(center);
		
		return ResponseModel.<Integer>success(id);
	}

	@Override
	protected IBaseService<Recharge> getBaseService() {
		return this.service;
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/update")
	public ResponseModel<Integer> update(@Validated @RequestBody RechargeUpdateParam param,
			HttpServletRequest httpServletRequest) {
		int res = this.service.parseUpdate(param, Recharge.class);
		return ResponseModel.<Integer>success(res);
	}

	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) RechargeVO.class;
	}
	
	@Resource
	public void setPreStoredRewardsConfService(IPreStoredRewardsConfService preStoredRewardsConfService) {
		this.preStoredRewardsConfService = preStoredRewardsConfService;
	}

	public IPayTypeService getPayTypeService() {
		return payTypeService;
	}

	@Resource
	public void setPayTypeService(IPayTypeService payTypeService) {
		this.payTypeService = payTypeService;
	}
}